#include "ShaderSettings.txt"
#include "common.h"
#include "lmodel.h"
#include "hmodel.h"
struct _input {
float4 hpos:POSITION;
#ifdef USE_VTF
float4 tc0:TEXCOORD0;
#else
float2 tc0:TEXCOORD0;
#endif
};
struct _out {
half4 low:COLOR0;
half4 high:COLOR1;
};
uniform sampler1D fog_table;
#ifndef USE_F32
_out main(_input I) {
float4 P=tex2D(s_position,I.tc0);
half4 N=tex2D(s_normal,I.tc0);
half4 D=tex2D(s_diffuse,I.tc0);
half4 L=tex2D(s_accumulator,I.tc0);
#ifdef USE_SUPER_SPECULAR
{
half ds=dot(D.rgb,1.h/3.h);
D.w=max(D.w,ds*ds/8.h);
}
#endif
#ifdef FORCE_GLOSS
D.w=FORCE_GLOSS;
#endif
#ifdef USE_GAMMA_22
D.rgb=(D.rgb*D.rgb);
#endif
half mtl=P.w;
#ifdef USE_R2_STATIC_SUN
half sun_occ=P.w*2;
mtl=xmaterial;
L+=Ldynamic_color*sun_occ*plight_infinity (mtl,P.xyz,N.xyz,Ldynamic_dir);
#endif
half3 hdiffuse,hspecular;
half occ=calc_ssao(P,N,I.tc0);
hmodel(hdiffuse,hspecular,mtl,N.w,D.w,P.xyz,N.xyz);
#ifdef SSAO_TEX_CONT
hdiffuse*=(D.rgb*0.95+CONT_COEF);
#endif
hdiffuse*=occ;
hspecular*=occ;
#ifndef SSAO
hmodel(hdiffuse,hspecular,mtl,N.w,D.w,P.xyz,N.xyz);
#endif
half4 light=half4(L.rgb+hdiffuse,L.w);
half4 C=D*light;
half3 spec=C.www+hspecular;
#ifdef USE_SUPER_SPECULAR
spec=(C.rgb*.5h+.5h)*C.w+hspecular;
#endif
half3 color=C.rgb+spec;
float3 pos=P.xyz;
float distance=length(pos);
float fog=saturate(distance*fog_params.w+fog_params.x);
color=lerp(color,fog_color,fog);
half skyblend=lerp(0,fog,pow(fog,5));
#ifdef DBG_TMAPPING
color=D.xyz;
#endif
#ifdef USE_VTF
half tm_scale=I.tc0.w;
#else
half tm_scale=tex2D(s_tonemap,I.tc0).x;
#endif
#ifdef USE_SUPER_SPECULAR
color=spec-hspecular;
#endif
#ifdef SATURATION_FILTER
float gray=dot(color.xyz,float3(0.3,0.59,0.11));
color.xyz=lerp(float3(gray,gray,gray),color.xyz,COLOR_SATURATION+(dot(Ldynamic_color.xyz,float3(1,1,1))/3)*COLOR_SAT_SUN_COEF);
float3 maxc=float3(1,0.62,0);
#endif
_out o;
tonemap(o.low,o.high,color,tm_scale);
o.low.a=skyblend;
o.high.a=skyblend;
return o;
}
#else
_out main(_input I)
{
float4 P=tex2D(s_position,I.tc0);
half4 N=tex2D(s_normal,I.tc0);
half4 D=tex2D(s_diffuse,I.tc0);
half4 L=tex2D(s_accumulator,I.tc0);
float F32=1;
#ifdef USE_SUPER_SPECULAR
{
half ds=dot(D.rgb,1.h/3.h);
D.w=max(D.w,ds*ds/8.h);
}
#endif
#ifdef FORCE_GLOSS
D.w=FORCE_GLOSS;
#endif
#ifdef USE_GAMMA_22
D.rgb=(D.rgb*D.rgb);
#endif
half mtl=P.w;
#ifdef USE_R2_STATIC_SUN
half sun_occ=P.w*2;
mtl=xmaterial;
L+=Ldynamic_color*sun_occ*plight_infinity(mtl,P.xyz,N.xyz,Ldynamic_dir);
#endif
half3 hdiffuse,hspecular;
half occ=calc_ssao(P,N,I.tc0);
hmodel(hdiffuse,hspecular,mtl,N.w,D.w,P.xyz,N.xyz);
#ifdef SSAO_TEX_CONT
hdiffuse*=(D.rgb*0.95+CONT_COEF);
#endif
hdiffuse*=occ;
hspecular*=occ;
#ifndef SSAO
hmodel(hdiffuse,hspecular,mtl,N.w,D.w,P.xyz,N.xyz);
#endif
D.w*=(D.w*mtl);
half4 light=half4((L.rgb*2)+hdiffuse,(L.w*2));
half4 C=D*light;
half3 spec=C.www+hspecular*C.rgba*light.a;
#ifdef USE_SUPER_SPECULAR
spec=(C.rgb*.5h+.5h)*C.w+hspecular;
#endif
half3 color=C.rgb+spec;
float3 pos=P.xyz;
float distance=length(pos);
float fog=saturate(distance*fog_params.w+fog_params.x);
color=lerp (color,fog_color,fog);
half skyblend=lerp(0,fog,pow(fog,5));
#ifdef USE_VTF
half tm_scale=I.tc0.w;
#else
half tm_scale=tex2D(s_tonemap,I.tc0).w;
#endif
#ifdef USE_SUPER_SPECULAR
color=spec-hspecular;
#endif
_out o;
tonemap (o.low,o.high,color,tm_scale);
o.low.a=skyblend;
o.high.a=skyblend;
return o;
}
#endif